import QtQuick 2.0
import SerialDataitem 1.0
import PrintLogitem 1.0
import ConfigFileitem 1.0

Rectangle {

    id:exhaleResult
    property double currentPpm: 0
    property double currentAirInfors:0
    property double currentPotencys: 0
    property string currentStrPotencys: "0"
    property int currentFrequencys:configFileitem.getRunNum()
    property double currentLimitNums: configFileitem.getLimitNum()

    signal sigCancelClick();
    signal sigCancelLimitClick();

    color: "#F2F2F2"

    function handSuccessBtn(){
        exhaleResult.sigCancelClick();
        printLogitem.printInfor("qml: exhaleResult sigCancelClick")
    }

    MouseArea{
        anchors.fill: parent
        onPressed: {
        }
        onReleased: {
        }
        onClicked: {
        }
    }

    onVisibleChanged: {

        if(visible){
            currentStrPotencys = "0";
            printLogitem.printInfor("qml: exhaleResult currentPpm=="+currentPpm)
            printLogitem.printInfor("qml: exhaleResult currentAirInfors=="+currentAirInfors)
            currentPotencys = Math.abs( currentPpm - currentAirInfors * currentFrequencys );
            if(currentPotencys > currentLimitNums){
                exhaleResult.sigCancelLimitClick();
                printLogitem.printInfor("qml: exhaleResult sigCancelLimitClick")

            }
            var tmps = currentPotencys.toFixed(2);
            currentStrPotencys = tmps+"";
            /*
            var tempsa=parseInt(currentPotencys % 10);
            var tempsb=parseInt(currentPotencys / 10);

            if(tempsa >= 5){
                tempsb += 1 ;
            }
            if(tempsb >=100){
                currentStrPotencys = parseInt(tempsb/100)+qsTr(".")+parseInt(tempsb%100)+ qsTr("");
            }else{
                if(tempsb >=10){
                    currentStrPotencys = parseInt(tempsb/100)+qsTr(".")+parseInt(tempsb%100)+ qsTr("");

                }else{

                    currentStrPotencys = parseInt(tempsb/100)+qsTr(".0")+parseInt(tempsb%10)+ qsTr("");



                }
            }
            */

            //            if(currentPotencys >=1000){
            //                currentStrPotencys = parseInt(currentPotencys/1000)+qsTr(".")+parseInt(currentPotencys%1000)+ qsTr("");
            //            }else{
            //                if(currentPotencys >=100){
            //                    currentStrPotencys = parseInt(currentPotencys/1000)+qsTr(".")+parseInt(currentPotencys%1000)+ qsTr("");

            //                }else{
            //                    if(currentPotencys >=10){
            //                        currentStrPotencys = parseInt(currentPotencys/1000)+qsTr(".0")+parseInt(currentPotencys%1000)+ qsTr("");

            //                    }else{
            //                        currentStrPotencys = parseInt(currentPotencys/1000)+qsTr(".00")+parseInt(currentPotencys%1000)+ qsTr("");

            //                    }

            //                }
            //            }

            printLogitem.printInfor("qml: exhaleResult currentPotencys=="+currentPotencys)
            printLogitem.printInfor("qml: exhaleResult currentStrPotencys=="+currentStrPotencys)
            handCurrentDate();
        }
    }



    function handCurrentDate(){
        var currentDate = new Date();
        var date_tring = currentDate.toLocaleDateString();
        //   console.log("local = ",date_tring,currentDate.getFullYear(),currentDate.getDay())
        var curyear = parseInt(currentDate.getFullYear().toString());
        var curmonth = parseInt((currentDate.getMonth()+1).toString())
        var curday = parseInt(currentDate.getDate().toString())


        var curhours= parseInt(currentDate.getHours());
        var curminute= parseInt(currentDate.getMinutes());
        var cursec= parseInt(currentDate.getSeconds());

        var curhourstr="";
        if(curhours < 10){
            curhourstr="0"+curhours;
        }else{
            curhourstr=""+curhours;
        }
        var curminutestr="";
        if(curminute < 10){
            curminutestr="0"+curminute;
        }else{
            curminutestr=""+curminute;
        }

        var cursecstr="";
        if(cursec < 10){
            cursecstr="0"+cursec;
        }else{
            cursecstr=""+cursec;
        }

        dateRightText.text = curyear+"年"+curmonth+"月"+curday+"日" ;
        dateText.text = curhourstr+":"+curminutestr+":"+cursecstr;
    }

    //顶部背景
    Rectangle{
        id:topBackground

        anchors.left: parent.left
        anchors.top: parent.top
        width: parent.width
        height: 276
        color: "#FFFFFF"
        Image {
            width: parent.width
            height: parent.height
            anchors.left: parent.left
            anchors.top: parent.top
            source: "qrc:/images/topbackgroundimage.png"
        }
        Text {
            id: topMacheneName
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.topMargin: 82
            width: parent.width
            height: 36
            color: "#FFFFFF"
            font.bold: true
            font.italic: true
            font.pixelSize: 33
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            text: qsTr("呼气分析仪")
        }
        Image {
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.topMargin: 67
            anchors.leftMargin: 59
            width: 48
            height: 48
            source: "qrc:/images/personimages.png"
        }
        Image {
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.topMargin: 72
            anchors.leftMargin: 64
            width: 38
            height: 38
            source: "qrc:/images/personinimages.png"
        }

        Text {
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.topMargin: 67
            anchors.leftMargin: 110
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignLeft
            width: 150
            height: 48
            color: "#FFFFFF"
            font.pixelSize: 22
            text: qsTr("Welcome")
        }

        Text {
            id: dateText
            anchors.left:  parent.left

            anchors.top: parent.top
            anchors.topMargin: 20
            height: 20
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            width: parent.width

            color: "#FFFFFF"
            font.pixelSize: 16
            text: qsTr("")
        }

        Text {
            id: dateRightText
            anchors.right:  parent.right
            anchors.rightMargin: 170
            anchors.top: parent.top
            anchors.topMargin: 20
            height: 20
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignRight
            width: 200
            color: "#FFFFFF"
            font.pixelSize: 16
            text: qsTr("")
        }

        Text {
            id: logText
            anchors.left:  parent.left
            anchors.leftMargin: 51
            anchors.top: parent.top
            anchors.topMargin: 116
            height: 25
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignLeft
            width: logText.contentWidth
            color: "#FFFFFF"
            font.pixelSize: 20
            font.bold: true
            font.italic: true
            text:  qsTr("KAMYRON")+qsTr("  ")+qsTr("卡米龙")
        }

        Timer {
            id: dateTimer
            interval: 1000;
            running: true;
            repeat: true
            onTriggered:{
                handCurrentDate();
            }
        }

    }


    //中间背景
    Rectangle{
        id:midBackground
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.right: parent.right
        anchors.leftMargin: 30
        anchors.topMargin: 176
        anchors.rightMargin: 30
        anchors.bottomMargin: 130
        color: "#FFFFFF"
        radius: 25

        Rectangle{
            id:midInforBackground
            width: parent.width
            height: parent.height
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.leftMargin: 5
            anchors.topMargin: 5

            radius: 10
            color: "#00000000"

            Item{
                anchors.topMargin: 30
                anchors.top: parent.top
                anchors.horizontalCenter: parent.horizontalCenter
                width: coTipText.width + 30 + coInforText.width+ coUintText.width
                height:50


                Text {
                    id: coTipText
                    anchors.left:  parent.left

                    anchors.top: parent.top

                    height: 50
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter
                    width: coTipText.contentWidth
                    color: "#E8C06B"
                    font.pixelSize: 30
                    font.bold: true
                    text: qsTr("呼出一氧化碳浓度:")
                }
                Text {
                    id: coInforText
                    anchors.left:  coTipText.right
                    anchors.leftMargin: 10
                    anchors.top: coTipText.top
                    height: 50
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignLeft
                    width: coInforText.contentWidth
                    color: "#E42929"
                    font.pixelSize: 35
                    font.bold: true
                    //   text:parseInt(currentPpm/1000)+qsTr(".")+parseInt(currentPpm%1000)+ qsTr("=")+parseInt(currentAirInfors/1000)+qsTr(".")+parseInt(currentAirInfors%1000)  //parseInt(currentPotencys/1000)+qsTr(".")+parseInt(currentPotencys%1000)+ qsTr("")
                    //         text:parseInt(currentPotencys/1000)+qsTr(".")+parseInt(currentPotencys%1000)+ qsTr("")
                    text:currentStrPotencys
                }
                Text {
                    id: coUintText
                    anchors.left:  coInforText.right
                    anchors.leftMargin: 10
                    anchors.top: coInforText.top
                    height: 50
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignLeft
                    width: coUintText.contentWidth
                    color: "#2A2A2A"
                    font.pixelSize: 35
                    font.bold: true
                    text: qsTr("PPM")
                }

            }

            Text {
                id:handTipText
                anchors.left:  parent.left
                anchors.leftMargin: 30
                width: parent.width
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 10
                height: 30
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignLeft

                color: "#999999"
                font.pixelSize: 20
                wrapMode: Text.WordWrap
                text: qsTr("注:ppm为体积浓度单位，即10^-6mol/mol")
            }


        }

        //        ClockDial{
        //            id:clockDial
        //            anchors.left:  parent.left
        //            anchors.leftMargin: 30
        //            anchors.verticalCenter:parent.verticalCenter
        //            // visible: exhaleResult.visible

        //        }
    }



    Rectangle{
        id:cancelBtn

        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 39
        width: 250
        height: 50
        radius: 25
        color:( cancelBtn.cancelBtnPressed ? "#5C24B9B7":"#FF24B9B7")

        property bool cancelBtnPressed: false
        Text {
            width: parent.width
            height: parent.height
            verticalAlignment: Text.AlignVCenter
            horizontalAlignment: Text.AlignHCenter
            font.pixelSize: 30
            color: "#FFFFFF"
            text: qsTr("返回")
        }

        MouseArea{
            anchors.fill: parent

            onPressed: {
                cancelBtn.cancelBtnPressed = true
            }
            onReleased: {
                cancelBtn.cancelBtnPressed = false
            }
            onClicked: {

                exhaleResult.sigCancelClick();
                printLogitem.printInfor("qml: exhaleResult sigCancelClick")
            }
        }

    }

    //    SerialDataitem{
    //        id:serialDataitem

    //    }

    PrintLogitem{
        id:printLogitem
    }

    ConfigFileitem{
        id:configFileitem
    }

}
